package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.DailyNews;
import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.mapper.DailyNewsMapper;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.query.DailyNewsQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyNewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class DailyNewsServiceImpl implements IDailyNewsService{

    @Autowired
    private DailyNewsMapper newsMapper;

    @Autowired
    private NewsContentMapper contentMapper;

    @Override
    public DailyNews selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public List<DailyNews> selectAll() {
        return null;
    }

    //新增或者修改
    @Override
    public void saveOrUpdate(DailyNews news) {
        //创建时间
        NewsContent content = news.getContent();
        if (news.getId()==null) {
            news.setCreateTime(new Date());
            news.setAuthorName("骡窝窝官方");
            newsMapper.insert(news);
            content.setNewsId(news.getId());
            contentMapper.insert(content);
        }else {
            newsMapper.updateByPrimaryKey(news);
            content.setNewsId(news.getId());
            contentMapper.updateByPrimaryKey(content);
        }
    }
    //页面分页方法
    @Override
    public PageInfo<DailyNews> queryForList(DailyNewsQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<DailyNews> list = newsMapper.selectForList(qo);
        return new PageInfo<>(list);
    }


    //查询index页面的日报方法
    @Override
    public PageInfo<DailyNews> selectForApp(DailyNewsQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<DailyNews> list = newsMapper.selectForApp(qo);
        return new PageInfo<>(list);
    }

    /**
     * 查询详细内容
     * @param news_id
     * @return
     */
    @Override
    public DailyNews selectByID(Long news_id) {
        DailyNews dailyNews = newsMapper.selectById(news_id);
        return dailyNews;
    }

    /**
     *  index.html页面的搜索栏需要的方法
     * @param qo 关键字
     * @return 全局查询的结果,只要求有一个
     */
    @Override
    public PageInfo<DailyNews> queryForDailyNews(QueryObject qo) {
        List<DailyNews> list = newsMapper.selectForDailyNews(qo);
        return new PageInfo<>(list);
    }


    /**
     * 查询5篇相同类型的文章
     * @return
     */
    @Override
    public PageInfo<DailyNews> selectByCatalog(DailyNewsQuery qo , Long id) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<DailyNews> dailyNews = newsMapper.selectByCatalog(qo,id);
        return new PageInfo<>(dailyNews);
    }


    @Override
    public NewsContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

}
